// pages/activity/index.js
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    userInfo: {},
    file_name: '',
    fileID: '',
    file_type: '',
    modalHidden: true
  },
  //打开比赛文件
  open_act_file(e) {
    var act_url = e.currentTarget.dataset.url
    console.log(act_url)
    wx.downloadFile({
      url: act_url,
      success: function (res) {
        var actpath = res.tempFilePath
        wx.openDocument({
          filePath: actpath,
          fileType: 'docx',
          success(res) {
            console.log("打开成功")
          },
          fail(err) {
            console.log(err)
          }
        })

      },
      fail(err){
        console.log(err)
      }
    })


  },
  //上传文件
  uploadFile(fileName, tempFile) {
    wx.cloud.uploadFile({
        cloudPath: 'act_works/' + fileName,
        filePath: tempFile,
      })
      .then(res => {
        console.log("文件上传成功", res);
        wx.showToast({
          title: '文件上传成功！',
        })
        this.setData({
          fileID: res.fileID
        }, () => {
          let data = {
            fileID: this.data.fileID,
            file_name: this.data.file_name,
          }
          wx.cloud.database().collection('actworks').add({
            data: data,
            success: (res) => {
              console.log(fileID)
            }
          })
        })
      })
      .catch(err => {
        console.log("文件上传失败", err);
      })

  },
  showFile: function () {
    //下载文件
    wx.cloud.downloadFile({
      fileID: this.data.fileID,
      success: res => {
        console.log("文件下载成功", res);
        //打开文件
        const filePath = res.tempFilePath
        if (this.data.file_type == 'image') {
          wx.previewImage({
            urls: [filePath],
            success: (res) => {
              console.log('文件打开成功', res)
            },
          })
        } else {
          wx.openDocument({
            filePath: filePath,
            success: (res) => {
              console.log('文件打开成功', res)
            },
            fail: (res) => {
              wx.showToast({
                title: '文件格式错误！',
              })
              console.error(res)
            }
          })
        }

      }
    })
  },
  deletefile: function () {
    this.setData({
      file_name: '',
      fileID: ''
    })
  },

  buttonTap: function () {
    this.setData({
      modalHidden: false
    })
  },

  /**
   * 点击取消
   */
  modalCandel: function () {
    // do something
    this.setData({
      modalHidden: true
    })
  },
  modalConfirm: function () {
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      success: (res) => {
        console.log(res)
        let tempFile = res.tempFiles[0]
        this.setData({
          file_name: util.subStrFormat(tempFile.name, 10),
          file_type: tempFile.type
        })
        this.uploadFile(tempFile.name, tempFile.path)
      },
    })
    this.setData({
      modalHidden: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo'),
      openid = wx.getStorageSync('OPENID')
    this.setData({
      openid: openid,
      userInfo: userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})